import { addCart } from "@/api/cart";
import { ElMessage } from "element-plus";
export default {
  namespaced: true,
  state: {
    cart: [],
  },
  mutations: {
    //购物车商品
    setCart(state, val) {
      console.log(val);
      state.cart = val;
    },
    //删除购物车商品
    delGoods(state, cid) {
      let index = state.cart.findIndex((item) => item.cid == cid);
      if (index >= 0) {
        state.cart.splice(index, 1);
      }
    },
    //修改数量
    setNumber(state, obj) {
      let index = state.cart.findIndex((item) => item.cid == obj.cid);
      if (index >= 0) {
        state.cart[index].number = obj.number;
      }
    },
  },
  actions: {
    //添加购物车
    async cartActions({ commit }, params) {
      let { data, code } = await addCart(params)
      console.log(data);
      if (code == 200) {
        commit("setCart", data);
        return true
      } else {
        ElMessage.error('服务器错误!!!请联系客服')
        return false
      }
    },
  },
};
